package eip.chapter10.processmanager.mule;

import org.apache.log4j.Logger;
import org.jbpm.JbpmConfiguration;
import org.jbpm.JbpmContext;
import org.jbpm.graph.def.ProcessDefinition;
import org.jbpm.graph.exe.ProcessInstance;

import eip.chapter10.processmanager.ScubaDivingBooking;
import eip.chapter10.processmanager.ScubaDivingRequest;

public class ScubaDivingProcessStarter {
	
	private final static Logger logger = Logger.getLogger(ScubaDivingProcessStarter.class);
	private static ProcessDefinition scubaProcess = ProcessDefinition.parseXmlResource(
			"eip/chapter10/processmanager/process/processdefinition.xml");

	public ScubaDivingBooking bookScubaDivingTrip(ScubaDivingRequest request) {
		logger.info("received a request for scuba diving with interest " + request.getInterest().toString());
		JbpmConfiguration configuration = JbpmConfiguration.getInstance();
		ProcessInstance processInstance = new ProcessInstance(scubaProcess);
		processInstance.getContextInstance().setVariable("booking", request);
		JbpmContext context = configuration.createJbpmContext();
		context.save(processInstance);
		processInstance.signal();
		ScubaDivingBooking booking = (ScubaDivingBooking) 
				processInstance.getContextInstance().getVariable("bookingResponse");
		return booking;
	}

}
